package struct;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class CtxServiceResult implements ServiceResult{
    private static final String ACTIVE_RULE_NUM_FIELD = "active_rule_number";
    private static final String RECEIVED_CTX_NUM_FIELD = "received_ctx_number";
    private static final String CHECKED_CTX_NUM_FIELD = "checked_ctx_number";
    private static final String RESOLVED_CTX_NUM_FIELD = "resolved_ctx_number";

    private Map<String, Long> result;

    public CtxServiceResult() {
        result = new HashMap<>();
    }

    public CtxServiceResult(long activeRuleNum, long receivedCtxNum, long ckeckedCtxNum, long resolvedCtxNum) {
        result = new HashMap<>();
        result.put(ACTIVE_RULE_NUM_FIELD, activeRuleNum);
        result.put(RECEIVED_CTX_NUM_FIELD, receivedCtxNum);
        result.put(CHECKED_CTX_NUM_FIELD, ckeckedCtxNum);
        result.put(RESOLVED_CTX_NUM_FIELD, resolvedCtxNum);
    }

    @Override
    public String toJSONString() {
        return toString();
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jo = new JSONObject();
        jo.putAll(result);
        return jo;
    }

    @Override
    public String toString() {
        JSONObject jo = new JSONObject();
        jo.putAll(result);
        return jo.toJSONString();
    }

    public static CtxServiceResult fromJSONString(String json) {
        CtxServiceResult res = new CtxServiceResult();
        JSONObject jo = JSON.parseObject(json);
        for (Map.Entry<String, Object> entry : jo.entrySet()) {
            res.result.put(entry.getKey(), Long.parseLong(entry.getValue().toString()));
        }
        return res;
    }

    public long getActiveRuleNum() {
        return result.getOrDefault(ACTIVE_RULE_NUM_FIELD, 0L);
    }

    public long getReceivedCtxNum() {
        return result.getOrDefault(RECEIVED_CTX_NUM_FIELD, 0L);
    }

    public long getCkeckedCtxNum() {
        return result.getOrDefault(CHECKED_CTX_NUM_FIELD, 0L);
    }

    public long getResolvedCtxNum() {
        return result.getOrDefault(RESOLVED_CTX_NUM_FIELD, 0L);
    }
}
